import java.awt.Color;
import java.util.ArrayList;


public class GameLogic {
	
	public int turnOrder;
	public ArrayList<Player> playerList;

	public GameLogic(){
		turnOrder=0;
		playerList = new ArrayList<Player>();
		playerList.clear();

	}
	
	public synchronized int addPlayer(Player p){
		playerList.add(p);
		p.setTurnOrder(playerList.size() - 1);
		return playerList.size() - 1;
	}
	
	public synchronized void removePlayer(int p) {
		//TODO: deal with pieces of removed player
		for(int i = p + 1; i < playerList.size(); i++)
			playerList.get(i).setTurnOrder(playerList.get(i).getTurnOrder() - 1);
		playerList.remove(p);
		//if(p == turnOrder)
		//	nextTurn();
	}
	
	public synchronized void nextTurn(){
		if(playerList.size() > 0)
			turnOrder = (turnOrder + 1) % playerList.size();
	}
	
	public Player getTurn(){
		return playerList.get(turnOrder);
	}

	public static boolean checkMove(GameBoard board, int x){
		if(x>=0  && x<board.getXDimension())
		{
			for(int y = 0; y < board.getYDimension(); y++)
			if(board.getPieceAt(x, y)==null)
				return true;
		}
		return false;
	}

	public static boolean checkForStalemate(GameBoard board){
		int topRow= board.getYDimension()-1;

		//check for pieces in the top row
		for(int x=0; x<board.getXDimension(); x++){			
			if(board.getPieceAt(x, topRow)==null)
				return false;
		}
		return true;
	}

	public static boolean checkForWin(GameBoard board, int x, int y){
		if(board.getPieceAt(x, y) == null)
			return false;
		Color candidate = board.getPieceAt(x, y).getColor();
		//check vertical
		for(int i=-2;i<1;i++){
			int matches=0;
			for(int j=0; j<3; j++){
				if( board.getPieceAt(x, (i+j+y))!=null &&board.getPieceAt(x, (i+j+y)).getColor().equals(candidate) )					
					matches++;					
				
			}	
			if(matches>=3)
				return true;			
		}
		
		//check horizontal
		for(int i=-2;i<1;i++){
			int matches=0;
			for(int j=0; j<3; j++){
				if( board.getPieceAt((i+j+x), y)!=null && board.getPieceAt((i+j+x), y).getColor().equals(candidate) )
					matches++;					
				
			}	
			if(matches>=3)
				return true;			
		}
		
		//check a diagonal
		for(int i=-2;i<1;i++){
			int matches=0;
			for(int j=0; j<3; j++){
				if( board.getPieceAt(i+j+x, i+j+y)!=null &&board.getPieceAt(i+j+x, i+j+y).getColor().equals(candidate) )
					matches++;					
				
			}	
			if(matches>=3)
				return true;			
		}
		
		//check another diagonal
		for(int i=-2;i<1;i++){
			int matches=0;
			for(int j=0; j<3; j++){
				if( board.getPieceAt(i+j+x, -i-j+y)!=null &&board.getPieceAt(i+j+x, -i-j+y).getColor().equals(candidate) )
					matches++;					
				
			}	
			if(matches>=3)
				return true;			
		}
		
		//3 in a row has not been found!
		return false;

	}

	public int getTurnOrder() {
		return turnOrder;
	}

	public synchronized void setTurnOrder(int turnOrder) {
		this.turnOrder = turnOrder;
	}

	public void reset() {
		turnOrder = 0;
		
	}

}
